package com.map.view;

/*

markdown控件
封装自火烧云的代码

*/
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.map.ui.BaseConfig;

import culosic.mdpocket.android.AppUtil;
import culosic.mdpocket.html.TransHtml;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MarkDownView extends WebView
{

    public MarkDownView(Context context){
        super(context);
    }


    /*
    加载markdown文本
    */
    public void loadMarkdown(String text)
    {

        String[] context = text.split("\n");
        ArrayList<String> array_text=new ArrayList<String>();
        for(String item:context)
        {
            array_text.add(item);
        }
        // 转换并加载
        TransHtml md = new TransHtml(array_text, AppUtil.getHtmlPre(getContext()));
        md.solve();
        md.showOn();

        this.loadDataWithBaseURL("file://", md.out.toString()
                , "text/html", "utf-8", "about:blank");
        setWebViewLink(this);

    }


    private void setWebViewLink(WebView t) {
        final Pattern p = Pattern.compile("^file:\\/\\/(.+)$");
        t.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Matcher m = p.matcher(url);
                if (m.find()) {
                    return true;
                } else {
                    // 网络

                    loadWebActivity(url);
                    return true;
                }
            }

            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
                WebResourceResponse response = null;
                Matcher m = p.matcher(url);
                if (m.find()) {
                    // 本地
                    //String p = MDPageHandler.bindPath(path, URLDecoder.decode(m.group(1)));
                    try {
                        //InputStream is = new FileInputStream(p);
                        //response = new WebResourceResponse("image/*", "", is);
                    } catch (Exception e) {
                    }
                }
                // 网络
                return response;
            }
        });
    }

    public boolean loadWebActivity(String url)
    {
//        Uri uri = Uri.parse( url);
//        Intent intent = new Intent(getContext(), WebActivity.class);
//        intent.setDataAndType(uri,"text/html");
//        getContext(). startActivity(intent);
        Intent intent=new Intent("android.intent.action.VIEW",Uri.parse(url));


        try
        {
            getContext().startActivity(intent);
            return true;
        }
        catch(Exception e)
        {
            Toast.makeText(getContext(),"请安装浏览器",Toast.LENGTH_SHORT).show();
        }
        return false;
    }

}
